package com.xt.base.exception;

/**
 * 系统错误 <br> *
 * 
 * <p>
 * <a href="SystemError.java.html"><i>�鿴Դ�ļ�</i></a>
 * </p>
 * 
 * @author ������
 * @version $Revision: $<br>
 *          $Id: $
 * 
 */
public class SystemError extends RuntimeException {

	private static final long serialVersionUID = -8430614662880647957L;

	/** 发生错误的对象 * */
	private Object whoOccuredError;

	/** 发生错误时方法的参数 * */
	private Object[] arguments;
	
	private static String UNKNOW = "未知";

	public SystemError(Object obj, Object[] args) {
		super();
		this.whoOccuredError = obj;
		this.arguments = args;
	}

	public SystemError(Object obj, Object[] args, String mes) {
		super(mes);
		this.whoOccuredError = obj;
		this.arguments = args;
	}

	public SystemError(Object obj, Object[] args, Throwable cause) {
		super(cause);
		this.whoOccuredError = obj;
		this.arguments = args;
	}

	public SystemError(Object obj, Object[] args, String mes, Throwable cause) {
		super(mes, cause);
		this.whoOccuredError = obj;
		this.arguments = args;
	}

	public String getObjectClassName() {
		return whoOccuredError == null ? UNKNOW : Class.class
				.isInstance(whoOccuredError) ? ((Class) whoOccuredError).getName()
				: whoOccuredError.getClass().getName();
	}

	public String getObjectArgumentsClasses() {
		StringBuilder builder = new StringBuilder();
		if (arguments != null) {
			for (int i = 0; i < arguments.length; i++) {
				builder
						.append(arguments[i] != null ? Class.class
								.isInstance(arguments[i]) ? ((Class) arguments[i])
								.getName()
								: arguments[i].getClass().getName()
								: "null");
				if (i < arguments.length - 1)
					builder.append(",");
			}
		} else
			builder.append("无");
		return builder.toString();

	}

	public String getLastExceptionClass() {
		StackTraceElement[] stack = getCause() == null ? new StackTraceElement[0]
				: getCause().getStackTrace();
		return stack.length > 0 ? stack[0].getClassName() : UNKNOW;
	}

	public String getLastExceptionMethodName() {
		StackTraceElement[] stack = getCause() == null ? new StackTraceElement[0]
				: getCause().getStackTrace();
		return stack.length > 0 ? stack[0].getMethodName() : UNKNOW;
	}

	public String getFirstExceptionClass() {
		StackTraceElement[] stack = getCause() == null ? new StackTraceElement[0]
				: getCause().getStackTrace();
		return stack.length > 0 ? stack[stack.length - 1].getClassName() : UNKNOW;
	}

	public String getFirstExceptionMethodName() {
		StackTraceElement[] stack = getCause() == null ? new StackTraceElement[0]
				: getCause().getStackTrace();
		return stack.length > 0 ? stack[stack.length - 1].getMethodName()
				: UNKNOW;
	}

	public String getStringInfo() {
		return new StringBuilder("错误来源:类[").append(
				getObjectClassName()).append("]参数[").append(
				getObjectArgumentsClasses()).append("]最后发生错误的类[").append(
				getLastExceptionClass()).append("]方法[").append(
				getLastExceptionMethodName()).append("]最初发生错误的类[").append(
				getFirstExceptionClass()).append("]方法[").append(
				getFirstExceptionMethodName()).append("]错误信息[").append(
				getMessage()).append("]").toString();
	}

	public String getExceptionInfo() {
		StringBuilder detail = new StringBuilder();
		Throwable throwable = getCause();
		while (throwable != null) {
			detail.append("<span style='color:blue;font-size:15'>").append(
					throwable.getClass().getName()).append(":</span>").append(
					throwable.getMessage());
			int i = 0, len = throwable.getStackTrace().length;
			for (StackTraceElement ste : throwable.getStackTrace()) {
				detail
						.append(
								"<br>&nbsp;&nbsp;<span style='color:black;font-size:15'>at:")
						.append(ste.getClassName()).append(".").append(
								ste.getMethodName()).append(
								"[<span style='color:blue;'>").append(
								ste.getFileName()).append(
								"&nbsp;&nbsp;" + ste.getLineNumber()).append(
								"</span>]</span>");
				if (i > 4) {
					detail
							.append("<span  style='font-size:15'><br>&nbsp;&nbsp;more ...("
									+ len + ")</span>");
					break;
				}
				i++;
			}
			throwable = throwable.getCause();
			if (throwable != null)
				detail.append("<br><span style='color:red'>Cause by:</span>");
		}
		return detail.toString();
	}

	/***************************************************************************
	 * @return 发生错误时方法的参数
	 **************************************************************************/
	public Object[] getArguments() {
		return arguments;
	}

	/***************************************************************************
	 * @return 发生错误的对象
	 **************************************************************************/
	public Object getWhoOccuredError() {
		return whoOccuredError;
	}
}
